package com.atguigu.gulimall.search.thread;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author houChen
 * @date 2021/11/28 15:25
 * @Description:
 */
public class CompletableFutureTest {

    public static ExecutorService executor = Executors.newFixedThreadPool(10);

    public static void main(String[] args) throws ExecutionException, InterruptedException {
     /* System.out.println("main start..");
        CompletableFuture<Void> fut = CompletableFuture.runAsync(() -> {
            System.out.println("当前线程：" + Thread.currentThread().getId());
            int i = 10 / 2;
            System.out.println("运行结果：" + i);
        }, executor);
        System.out.println("main end..");
        */

        /*System.out.println("main start..");
        CompletableFuture<Integer> futrue = CompletableFuture.supplyAsync(() -> {
            System.out.println("当前线程：" + Thread.currentThread().getId());
            int i = 10 / 0;
            System.out.println("运行结果：" + i);
            return i;
        }, executor).whenComplete((res,exception)->{
            //whenComplete 虽然能得到异常信息，但是无法修改返回结果
            System.out.println("异步任务执行完成，结果是："+ res + ";异常是："+exception);
        }).exceptionally(t -> {
            //exceptionally 可以感知异常，同时返回默认值
            return 10;  //
        });
        Integer res = futrue.get();
        System.out.println("main end.."+res);*/


        /*System.out.println("main start..");
        CompletableFuture<Integer> futrue = CompletableFuture.supplyAsync(() -> {
            System.out.println("当前线程：" + Thread.currentThread().getId());
            int i = 10 / 4;
            System.out.println("运行结果：" + i);
            return i;
        }, executor).handle((res,exception) -> {
            if(res!=null){
                return res * 2;
            }
            if(exception!=null){
                return 0;
            }
            return 0;
        });
        Integer res = futrue.get();
        System.out.println("main end.."+res);*/

       /* System.out.println("main start..");
        CompletableFuture.supplyAsync(() -> {
            System.out.println("当前线程：" + Thread.currentThread().getId());
            int i = 10 / 4;
            System.out.println("运行结果：" + i);
            return i;
        }, executor).thenAcceptAsync((res)-> {
            System.out.println("任务2启动了..."+res);
        },executor);
        System.out.println("main end..");*/

       /* System.out.println("main start..");
        CompletableFuture<Object> future1 = CompletableFuture.supplyAsync(() -> {
            System.out.println("任务1进程：" + Thread.currentThread().getId());
            int i = 10 / 4;
            System.out.println("任务1结束：" + i);
            return i;
        }, executor);

        CompletableFuture<Object> future2 = CompletableFuture.supplyAsync(() -> {
            System.out.println("任务2进程：" + Thread.currentThread().getId());
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("任务2结束");
            return "hello";
        }, executor);
*/
        //两任务组合（都要完成）
        /*future1.runAfterBothAsync(future2, ()->{
            System.out.println("任务3开始");
        },executor);
       */

        //f1,f2是future1、future2的返回结果
       /* future1.thenAcceptBothAsync(future2, (f1,f2)-> {
            System.out.println("任务3开始: future1 + future2 = "+  f1+f2);
        },executor);*/

       /* CompletableFuture<String> future3 = future1.thenCombineAsync(future2, (f1, f2) -> {
            System.out.println("任务3开始: future1 + future2 = ");
            return f1 + f2;
        }, executor);
         System.out.println("main end.."+future3.get());
         */

        //两任务组合（一个完成）
       /* future1.runAfterEitherAsync(future2, ()->{
            System.out.println("任务1，任务2只有一个完成");
        },executor);*/

        /*CompletableFuture<String> future = future1.applyToEitherAsync(future2, (res) -> {
            System.out.println("任务1，任务2只有一个完成");
            System.out.println("任务1|任务2 结果：" + res);
            return res + "hello";
        }, executor);
        System.out.println("main end:" + future.get());*/


        

    }
}